package com.asp.utils;

import com.asp.constant.SensitiveWordConstant;

import java.util.regex.Matcher;

/**
 * author:          ndg
 * date:            2024/3/29
 */
public class SensitiveWordUtils {
    private static final SensitiveWordConstant sensitiveWordConstant = new SensitiveWordConstant();

    public static String filter(String text) {
        Matcher matcher = sensitiveWordConstant.getPattern().matcher(text);
        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "***");  // 将匹配到的敏感词替换为"***"
        }
        matcher.appendTail(sb);  // 添加剩余部分到结果中
        return sb.toString();
    }

    public static boolean containsSensitiveWord(String text) {
        Matcher matcher = sensitiveWordConstant.getPattern().matcher(text);
        return matcher.find();  // 如果有匹配项，返回true；否则返回false
    }
}
